package queue;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //创建队列
        ArrayQueue arrayQueue = new ArrayQueue(3);
        char key = ' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列获取数据");
            System.out.println("h(head):查看队列头部数据");
            key = scanner.next().charAt(0);//接受的字符
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入1个数");
                    int n = scanner.nextInt();
                    arrayQueue.addQueue(n);
                    break;
                case 'g':
                    try {
                        int queue = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n", queue);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'h':
                    try {
                        int queue = arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n", queue);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;

                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }

    static class ArrayQueue {
        private int maxSize;//数组的最大容量
        private int front;//指向队列头
        private int rear;//指向队列尾
        private int[] arr;//模拟的队列

        //队列构造器
        public ArrayQueue(int arrmaxSize) {
            maxSize = arrmaxSize;
            arr = new int[maxSize];
            front = -1;//指向队列头的前一个位置
            rear = -1;
        }

        //判断是否满
        public boolean isFull() {
            return rear == front - 1;
        }

        //判断是否为空
        public boolean isEmpty() {
            return rear == front;
        }

        //添加数据
        public void addQueue(int n) {
            if (isFull()) {
                System.out.println("队列已满");
                return;
            }
            rear++;
            arr[rear] = n;
        }

        //获取数据
        public int getQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空，不能取数据。");
            }
            front++;
            return arr[front];
        }

        //显示队列的所有数据
        public void showQueue() {
            //遍历
            if (isEmpty()) {
                System.out.println("队列为空");
                return;
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.printf("arr[%d] = %d\n", i, arr[i]);

            }
        }

        //显示队列的头数据
        public int headQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列为空，不能取数据。");
            }
            return arr[front + 1];
        }
    }
}
